﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Web.Security;
using System.IO;

namespace University.Workshop.WebApp
{
	public partial class Bid : System.Web.UI.Page
	{
        Auction auction = new Auction();
        Product product = new Product();
		protected void Page_Load(object sender, EventArgs e)
		{
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            IdProductlb.Text=Request["id"];
            long productId = Int64.Parse(IdProductlb.Text);
            auction=BidManager.GetAuctionByproduct(productId);
            product = ProductDataAccess.GetProductById(productId);
            productName.Text = product.Name;
            TimeSpan ts = new TimeSpan();
            if (DateTime.Now < auction.Date)
                ts = (auction.Date-DateTime.Now);
            else
                ts = System.TimeSpan.Zero;
            expirationDate.Text = ts.ToString();
            lastBidPrice.Text = auction.LastBidPrice.ToString();
            currency.Text = product.Currency.ToString();
            currency0.Text = product.Currency.ToString();
            IdProductlb.Text = productId.ToString();
            //ProductInformation p = (ProductInformation)Page.LoadControl("~/ProductInformation.ascx");
            if (Directory.Exists(Server.MapPath("~/Thumbnails/") + product.ProductId + "/"))
            {
                
                DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~/Thumbnails/") + product.ProductId + "/");
                FileInfo[] currentImages = dir.GetFiles();
                string thumbUrl = "~/Thumbnails/" + product.ProductId + "/";
                foreach (FileInfo fi in currentImages)
                {
                    if (String.IsNullOrEmpty(image1.ImageUrl))
                    {
                        image1.ImageUrl = thumbUrl + fi.Name;

                    }
                }
            }
            else
            {
                image1.ImageUrl = "~/Thumbnails/NoImage/noimage.jpg";
            }

		}

        protected void placeBidbtn_Click(object sender, EventArgs e)
        {
            try
            {
                if (BidManager.EndAuction(auction.Date, Int64.Parse(IdProductlb.Text)) == AuctionState.Finished)
                {
                    Response.Redirect("BidClosed.aspx");
                }
                else if (newBidTxbx.Text != null)
                {
                    bool confirmation = BidManager.Bid(lastBidPrice.Text, newBidTxbx.Text,(UserDataAccess.GetUserByEmail(User.Identity.Name)).Id,IdProductlb.Text );
                    if (confirmation)
                    {
                        Response.Redirect("BidSuccess.aspx");
                    }
                }
            }
            catch (Exception er)
            {
                errorLbl.Text = er.Message;
            }
        }
	}
}